package service.impl;


import mapper.wenMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.wenxian;
import pojo.wenxian1;
import servlet.PageBean;
import servlet.du;
import util.SqlSessionFactoryUtils;

import java.sql.Date;
import java.util.List;

public class wenxianimpl implements wenxianSerice {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<wenxian1> cha(wenxian1 wenxian1) {
        SqlSession sqlSession = factory.openSession();
        //3. 获取UserMapper
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);
        String wen = wenxian1.getWen();
        if(wen != null && wen.length() > 0) {
            wenxian1.setWen("%"+wen+"%");
        }
        String guan = wenxian1.getGuan();
        if(guan != null && guan.length() > 0) {
            wenxian1.setGuan("%"+guan+"%");
        }
        String lei = wenxian1.getLei();
        if(lei != null && lei.length() > 0) {
            wenxian1.setLei("%"+lei+"%");
        }
        String zuo = wenxian1.getZuo();
        if(zuo != null && zuo.length() > 0) {
            wenxian1.setZuo("%"+zuo+"%");
        }
        //4. 调用方法
        List<wenxian1> cha = mapper.cha(wenxian1);

        //释放资源
        sqlSession.close();
        return cha;
    }

    @Override
    public void into(wenxian1 wenxian1) {
        SqlSession sqlSession = factory.openSession();
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);
        mapper.into(wenxian1);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public PageBean<wenxian1> selectByPageAndCondition(int currentPage, int pageSize, wenxian1 wenxian1) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        String zuo = wenxian1.getZuo();
        if (zuo!= null && zuo.length() > 0) {
            wenxian1.setZuo("%" + zuo+ "%");
        }
        String guan = wenxian1.getGuan();
        if (guan!= null && guan.length() > 0) {
            wenxian1.setGuan("%" + guan+ "%");
        }
        String lei = wenxian1.getLei();
        if (lei!= null && lei.length() > 0) {
            wenxian1.setLei("%" + lei+ "%");
        }
        //5. 查询当前页数据
        List<wenxian1> rows = mapper.selectByPageAndCondition(begin, size, wenxian1);
        //6. 查询总记录数
        int totalCount = mapper.selectTotalCountByCondition(wenxian1);
        //7. 封装PageBean对象
        PageBean<wenxian1> pageBean = new PageBean<pojo.wenxian1>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public void shan(String di) {
        SqlSession sqlSession = factory.openSession();
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);
        mapper.shan(di);
        sqlSession.commit();//提交事务
        sqlSession.close();
    }

    @Override
    public void xiu(wenxian1 wenxian1) {
        SqlSession sqlSession = factory.openSession();
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);
        mapper.xiu(wenxian1);
        sqlSession.commit();//提交事务
        sqlSession.close();
    }

    @Override
    public PageBean<wenxian1> shijian(int currentPage,int pageSize,Date kai, Date jie) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        wenMapper mapper = sqlSession.getMapper(wenMapper.class);


        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        List<wenxian1> shijian = mapper.shijian(begin,size,kai, jie);
        int totalCount = mapper.shis(kai,jie);
        //7. 封装PageBean对象
        PageBean<wenxian1> pageBean = new PageBean<pojo.wenxian1>();
        pageBean.setRows(shijian);
        pageBean.setTotalCount(totalCount);

        //8. 释放资源
        sqlSession.close();
        return pageBean;
    }


}
